class Solution
{
public:
    int maxOperations(vector<int> &nums, int k)
    {
        sort(nums.begin(), nums.end());
        int opCount = 0;
        int front = 0;
        int back = nums.size() - 1;
        while (front < back)
        {
            int sum = nums[front] + nums[back];
            if (sum == k)
            {
                ++opCount;
                ++front;
                --back;
            }
            else if (sum < k)
            {
                ++front;
            }
            else
            {
                --back;
            }
        }
        return opCount;
    }
};